/**
 * The GUI is in charge to manage Swing Components and their content
 * @author zulianp@lu.unisi.ch & lemmar@lu.unisi.ch
 * @version 0.1
 */



package ch.unisi.inf.projects.java.uom.gui; 

import java.util.Set;
import java.awt.*;
import javax.swing.*;

import ch.unisi.inf.projects.java.uom.elements.*;
import ch.unisi.inf.projects.java.uom.gui.commands.SelectNextItemDisplayedCommand;
import ch.unisi.inf.projects.java.uom.gui.commands.SelectPreviousItemDisplayedCommand;
import ch.unisi.inf.projects.java.uom.gui.itemeditor.ItemEditor;
import ch.unisi.inf.projects.java.uom.gui.itemeditor.ItemEditorMemoryStack;
import ch.unisi.inf.projects.java.uom.gui.table.UOMItemClassJTable;
import ch.unisi.inf.projects.java.uom.gui.table.UOMItemJTable;
import ch.unisi.inf.projects.java.uom.gui.table.item.UOMItemJTableListener;
import ch.unisi.inf.projects.java.uom.gui.table.itemclass.UOMItemClassJTableListener;


public final class GUI
{
    
    @SuppressWarnings("unused")
	private final ItemManager itemManager;
    private UOMMainFrame frame;
    
   
    public GUI() 
    {
        try { 
                // Set System L&F
                UIManager.setLookAndFeel(
                    UIManager.getSystemLookAndFeelClassName());
            } 
            catch (final UnsupportedLookAndFeelException ex) {
               assert false;
            }
            catch (final ClassNotFoundException ex) {
               assert false;
            }
            catch (final InstantiationException ex) {
               assert false;
            }
            catch (final IllegalAccessException ex) {
               assert false;
               //TODO eventually
            }
            
            itemManager = ItemManager.instance();
            makeFrame();
            
    }

    
  
    private void makeFrame() 
    {
    	frame = UOMMainFrame.instance();     
    	final Container contentPane = frame.getContentPane();
        contentPane.setLayout(new BorderLayout());
        
        //ToolBar set-up
        ToolBar toolBar = new ToolBar();
        contentPane.add(toolBar, BorderLayout.NORTH);
        toolBar.setMaximumSize(new Dimension(200,1000));
        
       
		
		// list panel
       	final UOMItemClassJTable itemClassesList = new UOMItemClassJTable();
       	final JPanel listPanel = createItemClassListPanel(itemClassesList);
       	
        
		final JPanel form = new JPanel();
       	final UOMItemJTable itemTable = new UOMItemJTable(form);
       	itemClassesList.addUOMItemClassJTableListener(itemTable);
       	final JPanel itemPanel = createItemPanel(itemTable,form);
       	
       	final ItemEditor itemEditor = new ItemEditor(null, itemTable);
        itemEditor.addListener(toolBar);
       	//item editor added as a listener (has to be the last notified)
       	itemTable.addUOMItemJTableListener(itemEditor); 
        itemClassesList.addUOMItemClassJTableListener(itemEditor); 
        toolBar.addUOMToolBarListener(itemEditor);
        itemEditor.addListener(itemTable);
        //memoryStack for itemEditor
        final ItemEditorMemoryStack memoryStack = new ItemEditorMemoryStack(itemEditor);
        final SelectPreviousItemDisplayedCommand selectPrevious = new SelectPreviousItemDisplayedCommand(memoryStack);
        selectPrevious.addCommandListener(itemTable);
        memoryStack.addListener(selectPrevious);
        toolBar.addItemRelatedCommand(selectPrevious);
       	final SelectNextItemDisplayedCommand selectNext = new SelectNextItemDisplayedCommand(memoryStack);
       	toolBar.addItemRelatedCommand(selectNext);
       	memoryStack.addListener(selectNext);
        //SplitPanes set-up
        final JSplitPane JSPV = new JSplitPane(JSplitPane.VERTICAL_SPLIT,itemEditor, itemPanel);
        final JSplitPane JSPH = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,listPanel,JSPV);
        JSPV.setDividerLocation(400);
        JSPV.setDividerSize(3);
        JSPH.setDividerSize(3);
        JSPH.setEnabled(false);
        contentPane.add(JSPH,BorderLayout.CENTER);
      
        frame.setResizable(false);
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
        
       
    }
	    
    private JPanel createItemPanel (final UOMItemJTable itemTable, final JPanel form)
    {
    	final JPanel itemPanel = new JPanel();
       	itemPanel.setLayout(new BoxLayout(itemPanel,BoxLayout.Y_AXIS));
       	  	
        final JScrollPane scrollPane = new JScrollPane(itemTable);
        scrollPane.setPreferredSize(new Dimension(250,1500));
        itemPanel.add(scrollPane);
       	final SpringLayout layout = new SpringLayout();
      	form.setLayout(layout);
       	final JTextField filterTexts = new JTextField();
       	filterTexts.getDocument().addDocumentListener(itemTable);
       	form.add(filterTexts);
       	form.setPreferredSize(new Dimension(25,36));
       	form.setMinimumSize(new Dimension(25,36));
       	layout.putConstraint(SpringLayout.WEST, filterTexts, 5, SpringLayout.WEST, form);
       	layout.putConstraint(SpringLayout.NORTH, filterTexts, 5, SpringLayout.NORTH, form);
       	layout.putConstraint(SpringLayout.EAST, form, 5, SpringLayout.EAST, filterTexts);
       	layout.putConstraint(SpringLayout.SOUTH, form, 5, SpringLayout.SOUTH, filterTexts);
       	itemPanel.add(form);
       	itemTable.addUOMItemJTableListener(new UOMItemJTableListener() {
        	public void itemSelected(UOMItemJTable table, Item i) { return; }
        	public void searchChanged (UOMItemJTable table, boolean state) { return; }
        	public void tableHidden (UOMItemJTable table) { form.setVisible(false); }
        	public void tableShown (UOMItemJTable table) { form.setVisible(true); form.repaint(); } });
         return itemPanel;
    }
    
	private JPanel createItemClassListPanel (final UOMItemClassJTable itemClassesList)
    {    	
    	final JPanel listPanel = new JPanel();
       	listPanel.setLayout(new BoxLayout(listPanel,BoxLayout.Y_AXIS));
       	final JPanel form = new JPanel();
       	final SpringLayout layout = new SpringLayout();
       	form.setLayout(layout);
       	final JTextField filterText = new JTextField();
       	filterText.getDocument().addDocumentListener(itemClassesList);
       	form.add(filterText);
       	layout.putConstraint(SpringLayout.WEST, filterText, 5, SpringLayout.WEST, form);
       	layout.putConstraint(SpringLayout.NORTH, filterText, 5, SpringLayout.NORTH, form);
       	layout.putConstraint(SpringLayout.EAST, form, 5, SpringLayout.EAST, filterText);
       	layout.putConstraint(SpringLayout.SOUTH, form, 5, SpringLayout.SOUTH, filterText);  	
        final JScrollPane scrollPane = new JScrollPane(itemClassesList);
        scrollPane.setPreferredSize(new Dimension(250,1500));
        listPanel.add(scrollPane);
        listPanel.add(form);
        itemClassesList.addUOMItemClassJTableListener(new UOMItemClassJTableListener() {
        	public void itemClassesSelected(UOMItemClassJTable table, Set<ItemClass> itemClasses) { return; }
        	public void tableHidden (UOMItemClassJTable table) { form.setVisible(false); }
        	public void tableShown (UOMItemClassJTable table) { form.setVisible(true); form.repaint(); } });
        return listPanel;
    }  
    
}
